
package com.geofencing.controller;

import com.geofencing.model.Geofence;
import com.geofencing.model.GeofenceDTO;
import com.geofencing.service.GeofenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/geofences")
public class GeofenceController {
    private final GeofenceService geofenceService;

    @Autowired
    public GeofenceController(GeofenceService geofenceService) {
        this.geofenceService = geofenceService;
    }

    @PostMapping("/create")
    public ResponseEntity<Geofence> createGeofence(
            @RequestParam String name,
            @RequestParam String polygon) {
        try {
            Geofence savedGeofence = geofenceService.saveGeofence(name, polygon);
            return ResponseEntity.ok(savedGeofence);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/contains")
    public List<GeofenceDTO> findGeofenceContainingPoint(
            @RequestParam double longitude,
            @RequestParam double latitude) {
        List<GeofenceDTO> geofence = geofenceService.findGeofenceContainingPoint(longitude, latitude);
        return geofence;
    }
}
